package com.joysuch.wwyt.common.controller;

import com.joysuch.wwyt.common.bean.NotifyQueryBean;
import com.joysuch.wwyt.common.service.CommonIndexNotifyService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息通知
 *
 * @Author ZhangDong
 * @Date 2020/5/29 12:19
 */
@RestController
@RequestMapping("/common/index/notify")
@Api(tags = "/common/index/notify 首页通知")
public class CommonIndexNotifyController {

    @Autowired
    private CommonIndexNotifyService commonIndexNotifyService;


    /**
     * 前端每隔5-10秒调接口，查询当前用户消息通知
     */
    @GetMapping("getNotify")
    @ApiOperation("查询当前用户消息通知")
    public ResultBean getNotify() {
        return commonIndexNotifyService.getNotify();
    }

    /**
     * 菜单已读
     */
    @PostMapping("readed")
    @ApiOperation("菜单已读")
    @RequiresPermissions("common:index:notify:readed")
    public ResultBean readed(@RequestBody List<Long> list) {
        return commonIndexNotifyService.readed(list, 0);
    }

    /**
     * 首页已读
     */
    @PostMapping("read-home")
    @ApiOperation("首页已读")
    public ResultBean readHome(@RequestBody List<Long> list) {
        return commonIndexNotifyService.readed(list, 0);
    }

    /**
     * 首页全部已读
     * @return
     */
    @GetMapping("all-read-home")
    @ApiOperation("首页全部已读")
    public ResultBean allReadHome() {
        return commonIndexNotifyService.readed(null, 1);
    }

    @PostMapping("page")
    @ApiOperation("分页查询，remindDate:提醒日期; readFlag: 0 未读; 1 已读; name: 序号/模块名称")
    public ResultBean page(@RequestBody NotifyQueryBean bean) {
        Pageable pageable = QPageRequest.of(bean.getPage(), bean.getSize());
        return commonIndexNotifyService.page(pageable, bean);
    }

    @GetMapping("allReaded")
    @ApiOperation("页面，全部已读")
    @RequiresPermissions("common:index:notify:allReaded")
    public ResultBean allReaded() {
        return commonIndexNotifyService.readed(null, 1);
    }


    /**
     * 即时调用Schedule Job ,启动生成消息通知
     */
    @GetMapping("generateNotify")
    @ApiOperation("生成消息通知")
    public ResultBean generateNotify()
    {
       commonIndexNotifyService.generateNotifyTask();
       return ResultBean.defaultSuccessResult();
    }


    /**
     * 即时调用Schedule Job ,启动生成消息通知
     */
    @GetMapping("generateNotifyJob")
    @ApiOperation("生成消息通知Job")
    public ResultBean generateNotifyJob()
    {
        commonIndexNotifyService.generateNotifyJob();
        return ResultBean.defaultSuccessResult();
    }
}
